function setItemVisible(region,item,visibled){
	item=item.toUpperCase();
	visibled?$("#"+region+" ."+item).show():$("#"+region+" ."+item).hide();
}

/** 
* 获取指定栏目的值 
*/ 
function getItemValue(obj, item) { 
	item=item.toUpperCase();
	if(obj=='dwForm'){
		region='#dwForm';
	}else{
		region=obj;
	}
	//判断当前region内是否存在dwInfo样式，即是否为Info页面
	if($(region+' .dwInfo').length>0){
		var value;
		//按照radio读取
		if($(":radio[name='O."+item+"']").length>0){
			value=$(":radio[name='O."+item+"']:checked").val();
		}else{
			value=$(region+" #"+item).val();
		}
		return value;
	}else{
		var ob =$(region);
		if(ob.length==0&&obj=='dwForm'){
			region=".dwForm";
			ob =$(region);
		}
		var selected=ob.length>0?ob.datagrid('getSelected'):null;
		if (selected){ 
			return selected[item];//easyUI List样式 
		}else{ 
			return $( region + " .tdclick *[class=" + item + "]").val();//自创的List样式 
		} 
	}
} 

/** 
* 设置指定栏目的值 
*/ 
function setItemValue(region, item,val) { 
	item=item.toUpperCase();
	//判断当前region内是否存在dwInfo样式，即是否为Info页面
	if($('#'+region+' .dwInfo').length>0){
		var obj=$("#"+region+" #"+item);
		$("#"+region+" #"+item).val(val);
		if($(obj).is('input[type=hidden]')){
			$("#"+region+" #span_"+item).text(val);
		}
	}else{
		var selected = $('.'+region).datagrid('getSelected'); 
		if (selected){ 
			var index=$('.'+region).datagrid('getRowIndex',selected); 
			var field={};
			field[item]=val;
			var ed=$('.'+region).datagrid('updateRow', {index:index,row:field});
		}else{ 
			$('.' + region + " .tdclick *[class=" + item + "]").val(val);//自创的List样式 
		} 
	}
}

/**
 * 刷新当前页面
 */
function reloadSelf(obj) {
	obj=obj?$(obj):$('.dwForm');
	if(obj){
		for(var i=0;i<obj.length;i++){
		$(obj[i]).datagrid('reload');
		}
		/*var script=$('script');
		for(var i=0;i<script.length;i++){
			text=script[i].text;
			if(text&&text!=""){
				eval(script[i].text);
			}
		}*/
	}else{
		self.location.reload();
	}
}
var DWControl = {
	_getDefaultOpenStyle : function() {
		return "width="
				+ screen.availWidth
				+ "px,height="
				+ screen.availHeight
				+ "px,top=0,left=0,toolbar=no,scrollbars=yes,resizable=yes,status=no,menubar=no";
	},
	_getDefaultDialogStyle : function() {
		return "dialogWidth=" + screen.availWidth + "px;dialogHeight="
				+ screen.availHeight
				+ "px;resizable=yes;maximize:yes;help:no;status:no;";
	},
	_getDialogStyle : function(sStyle) {
		if (typeof (sStyle) == "undefined" || sStyle == "")
			return this._getDefaultDialogStyle();
		else
			return sStyle;
	},
	_getParaString : function(sPara) {
		if (typeof (sPara) == "undefined" || sPara == "") {
			return "";
		} else if (sPara.substring(0, 1) == "&") {
			return encodeURI(encodeURI(sPara));
		} else {
			return "&" + encodeURI(encodeURI(sPara));
		}
	},
	randomNumber : function() {
		return Math.abs(Math.sin(new Date().getTime())).toString().substr(2);
	}
};

/**
 *过时方法 
 **/
DWControl.PopDwComp=function(sURL, sPara, sStyle){
	//return DWControl.PopComp("comm/datawindow_page.jsp","pagePath="+encodeURI(sURL)+"&"+sPara,sStyle);
	return DWControl.PopComp("datawindow_page.html","pagePath="+encodeURI(sURL)+"&"+sPara,sStyle);
};

/**
 *过时方法 
 **/
DWControl.PopComp = function(sURL, sPara, sStyle) {
	if (sURL.indexOf("?") >= 0) {
		alert("URL中存在\"?\"！");
		return false;
	}
	var sDialogStyle = this._getDialogStyle(sStyle);
	alert("不推荐使用");
	//return window.showModalDialog(sWebRoot + sURL + "?" +encodeURI(sPara), "",
	//		sDialogStyle);
};

DWControl.OpenDwComp=function(sURL, sPara, sTarget){
	//return DWControl.OpenComp("comm/datawindow_page.jsp","pagePath="+encodeURI(sURL)+"&"+sPara,sStyle); 
	return DWControl.OpenComp("datawindow_page.html","pagePath="+encodeURI(sURL)+"&"+sPara,sTarget);
};

DWControl.OpenComp = function(sURL, sPara, sTarget) {
	/*if (sURL.indexOf("?") >= 0) {
		alert("URL中存在\"?\"！");
		return false;
	}*/
	_.i(sWebRoot + sURL + "?" + sPara);
	if (sTarget) {
		if (undefined != sPara) {
			if(sURL.indexOf("?") >= 0){
				sTarget.location = sWebRoot + sURL + "&"
				+ sPara;
				
			}else{
				sTarget.location = sWebRoot + sURL + "?"
					+ sPara;// (sWebRoot+sURL+"?"+sPara,"",sDialogStyle);
			}
		} else {
			sTarget.location = sWebRoot + sURL;
		}
	} else {
		if (undefined != sPara) {
			if(sURL.indexOf("?") >= 0){
				location.href = sWebRoot + sURL + "&" + sPara;
			}else{
				location.href = sWebRoot + sURL + "?" + sPara;// (sWebRoot+sURL+"?"+sPara,"",sDialogStyle);
			}
		} else {
			location.href = sWebRoot + sURL;
		}
	}
};

DWControl.save = function(form,callback) {
	if(form && typeof(form) === "function"){
		callback=form;
		form=null;
	}
	if (null == form || undefined == form) {
		form = "#dwForm";
	}
	//绑定表单中的ckeditor数据
	if(typeof(CKEDITOR)!="undefined"){
		for ( instance in CKEDITOR.instances ) {
			CKEDITOR.instances[instance].updateElement(); 
		}
	}
	//表单校验:必输项是否为空
	var objArray=$('.required');
	/*var pause=false;
	for(var i=0;i<objArray.length;i++){
		if($.trim($(objArray[i]).val())==""){
			$(objArray[i]).parents('td').find('.missingMessage').html("必输项不能为空");
			pause=true;
		}else{
			$(objArray[i]).parents('td').find('.missingMessage').html("");
		}
	}
	if(pause){
		return;
	}*/
	
	$.messager.progress({title:'提示',msg:'正在提交,请稍后',text:'提交中...'});
	jQuery.ajax({
		url : sWebRoot + "dwServlet",// ?action=save&" + $("#" +
										// form).serialize(), // 提交的页面
		data : "action=save&" + $(form).serialize(), // 从表单中获取数据
		type : "POST", // 设置请求类型为"POST"，默认为"GET"
		dataType :"json",
		beforeSend : function() // 设置表单提交前方法
		{
			var isValid = $(form).form('validate');
			if (!isValid){
				$.messager.progress('close');	// hide progress bar while the form is invalid
			}
			return isValid;	
		},
		error : function(request) { // 设置表单提交出错
			alert("表单提交出错，请稍候再试");
			$.messager.progress('close');
		},
		async : false,
		cache : false,
		success : function(data) {
			$.messager.progress('close');
			if (!data['success']) {
				alert(data['message']);
			} else {
				//更新页面属性为update
				$("input[name='O.isinsert'").val('false');
				for(var d in data['data']){
					var json=eval('('+data['data'][d]+")");
					for(var attr in json){  
						setItemValue("dwForm", attr, json[attr]);
					}
				}
				if(callback && typeof(callback) === "function"){
					callback(data);
				}else{
					alert("保存成功!");
				}
			}
			_.i("表单提交结果:" + data);
		}
	});
};

/**
 *参数暂时无效 
 */
DWControl.Delete=function(form){
	
	if (null == form || undefined == form) {
		form = "dwForm";
	}
	//获取当前显示模板主表的主键
	var doNo;
	if(form=='dwForm'){
		doNo=$('.'+form).attr('doNo');
	}else{
		doNo=$(form).attr('doNo');
	}
	var keyFileds=RunJavaMethod("platform.dataWindowAction", "getTableKeysByDoNo", {doNo:doNo}).split(",");
	selectParam="";
	for(var i=0;i<keyFileds.length;i++){
		var val=getItemValue(form, keyFileds[i]);
		if(val==null){
			alert("请选中一条记录或该条记录无法识别主键!");
			return ;
		}
		selectParam=selectParam+keyFileds[i]+"="+val+"&";
	}
	if(!confirm("确定要删除选中的记录吗?")){
		return;
	}
	return jQuery.ajax({
		url : sWebRoot + "dwServlet",
		data : "action=delete&doNo="+doNo+"&" + selectParam, // 从表单中获取数据
		type : "POST", // 设置请求类型为"POST"，默认为"GET"
		beforeSend : function() // 设置表单提交前方法
		{
		},
		error : function(request) { // 设置表单提交出错
			alert("表单提交出错，请稍候再试"+request);
		},
		async : false,
		cache : false,
		dataType :"json",
		success : function(data) {
			if(data['success']){
				alert('删除成功!');
				$("input[name='O.isinsert'").val('false');
			}else{
				alert(data['message']);
			}
			_.i("表单提交结果:" + data);
		}
	}).responseText;
};

DWControl.exportExcel = function(form) {
	if (null == form || undefined == form) {
		form = "dwForm";
	}
	//获取当前显示模板主表的主键
	var doNo;
	if(form=='dwForm'){
		doNo=$('.'+form).attr('doNo');
	}else{
		doNo=$(form).attr('doNo');
	}
	var queryParam=$('#'+doNo).datagrid('options')['queryParams'];
	//alert(sWebRoot+"dwExportExcel?doNo="+doNo+"&whereSql="+encodeURI(queryParam['whereSql'])+"&paramVal="+encodeURI(queryParam['paramVal']||""));
	//return;
	location.href=sWebRoot+"dwExportExcel?doNo="+doNo+"&whereSql="+encodeURI(queryParam['whereSql'])+(queryParam['paramVal']==null?"":"&paramVal="+encodeURI(queryParam['paramVal']));
};

/**
 *弹出选择页面
 *doNo:显示模板编号
 *params：显示模板依赖的查询条件
 *isMulti:是否多选
 *returnField：返回字段
 *callback:父窗口回调事件名称
 **/
DWControl.PopSelectort=function(doNo,params,isMulti,returnField,callback){
	var option={};
	option['href']='frame/dwSelector.jsp';
	option['param']="doNo="+doNo+"&isMulti="+isMulti+"&returnField="+returnField+"&params="+params+"&callback="+callback;
	option['title']="选择";
	option['width']=400;
	option['height']=400;
	DWControl.Window(option);
};

DWControl.PopTreeSelectort=function(treeNo,isMulti,returnField,callback){
	var option={};
	option['href']='frame/treeSelector.jsp';
	option['param']="treeNo="+treeNo+"&isMulti="+isMulti+"&returnField="+returnField+"&callback="+callback;
	option['title']="选择";
	option['width']=400;
	option['height']=400;
	DWControl.Window(option);
	//var obj=DWControl.PopComp("frame/treeSelector.jsp","treeNo="+treeNo+"&isMulti="+isMulti+"&returnField="+returnField,sStyle);
	//if(obj!=undefined&&"_CLEAR_"!=obj){
	//    obj=eval('('+obj+')');
	//}
	//return obj;
};

DWControl.OpenDwWindow=function(option,callback){
	option=option||{};
	//option['href']='comm/datawindow_page.jsp';
	option['href']='datawindow_page.html';
	option['param']="pagePath="+encodeURI(option['page'])+"&"+option['param'];
	DWControl.Window(option,callback);
};

DWControl.Window=function(option,callback){
	var id=option['id'];
	var w=$('#'+id);
	var length=w.length;
	if(length==0){
		$('body').append("<div id='"+id+"'></div>");
		w=$('#'+id);
	}
	var href;
	var sURL=option['href'];
	var sPara=option['param'];
	if (undefined != sPara) {
		if(sURL.indexOf("?") >= 0){
			href = sWebRoot + sURL + "&"+ sPara;
			
		}else{
			href = sWebRoot + sURL + "?"+ sPara;
		}
	} else {
		href = sWebRoot + sURL;
	}

	var con = '<iframe name="'+id+'_iframe" style="width:100%;height:100%;border:0;" src=""></iframe>';
	$('#'+id).html(con);
	
	var maximized=(option['width']||option['height'])==null;
	$('#'+id).window({   
	    width:option['width']||"auto",
	    //href:href,
	    maximized:maximized,
	    height:option['height']||"auto",   
	    modal:true,
	    title:option['title']||null,
	    minimizable:false,
	    collapsible:false,
	    onOpen:function(){
	    	$('#'+id+" iframe").attr('src',href);
	    	$('.window-mask').dblclick(function(){
	    		if($(this).prevUntil('.window').length==1){
	    			$('#'+id).window('close');
	    		}
	    	});
	    },
	    onClose:function(){
	    	(callback && typeof(callback) === "function") && callback();
	    	$('#'+id).window('destroy');
	    }
	}); 
};